package com.example.controller;

import com.example.service.UserService;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;
@Controller
@RequestMapping
public class RegisterController {

    @Autowired
    UserService userService;

    @RequestMapping("register")
    public String register(){
        return "register";
    }
    @RequestMapping("registerAdd")
    public String registerAdd(String name ,String password , Model model) throws Exception {
        String msg = null;
        if(!HtmlUtils.htmlEscape(name).equals(name)){
            msg = "用户名含有特殊字符，无法注册，请重新输入";
            model.addAttribute("msg",msg);
            return "register";
        }else if(userService.isExist(name)){
            msg = "用户名已存在，请更换";
            model.addAttribute("msg",msg);
            return "register";
        }
        else{
            com.example.model.User user = new User();
            user.setName(name);
            user.setPassword(password);
            userService.register(user);
            return "registerSuccess";
        }
    }
}
